# -*- coding: utf-8 -*-

from osv import fields,osv
from osv.orm import browse_record
import tools
from functools import partial
import pytz
import pooler
from tools.translate import _
from service import security
import netsvc

class aas_frame_result_detail(osv.TransientModel):
    _name = 'aas.frame.result.detail'
    _columns = {
        'result_id': fields.many2one('aas.frame.result', string='Frame Result', required=False),
        'name': fields.char('/', size=8),
        'semester': fields.char('Học kỳ thứ', size=64),
        'subject_code': fields.char('Mã môn học', size=64),
        'subject_name': fields.char('Tên môn học', size=64),
        'type': fields.char('Loại môn học', size=64),
        'total_credit': fields.integer('Số tín chỉ tham khảo'),
        'course_code': fields.char('Mã lớp', size=64),
        'total_credit_course': fields.integer('Số tín chỉ đã học'),
        'mark': fields.char('Điểm', size=64),
        'eqvl': fields.char('Học môn tương đương', size=128),
    }
    _defaults = {
        'name': lambda *a: '/',
    }
    _order = 'semester'
aas_frame_result_detail()

class aas_frame_result(osv.TransientModel):
    _name = 'aas.frame.result'
    _description = 'Kết quả học tập'
    _columns = {
        'student_id': fields.many2one('res.users', string='Sinh viên', domain=[('type', '=', 'student')], required=True),
        'readonly': fields.boolean('Readonly'),
        'details': fields.one2many('aas.frame.result.detail', 'result_id', string='Chi tiết', readonly=True),
        'total_credit_required': fields.integer('Số tín chỉ yêu cầu', readonly=True),
        'total_credit_gained': fields.integer('Số tín chỉ đã tích lũy', readonly=True),
        'finished': fields.boolean('Đã hoàn thành chương trình đào tạo?', readonly=True),
        'percentage': fields.char('Tỷ lệ hoàn thành', size=16, readonly=True),
    }
    _defaults = {
    }
    
    def compute(self, cr, uid, id, context=False):
        res = {}
        user_obj = self.pool.get('res.users')
        detail_obj = self.pool.get('aas.frame.result.detail')
        
        student_id = uid
        
        if id:
            obj = self.browse(cr, uid, id, context=context)
        
            student_id = obj.student_id.id or uid
        
        all_details = detail_obj.search(cr, uid, [])
        detail_obj.unlink(cr, uid, all_details)
        
        frame_results = user_obj.get_frame_result(cr, uid, student_id, context=context)
        details = []
        for line in frame_results['details']:
            detail_data = {
                'semester': line[0],
                'subject_code': line[1],
                'subject_name': line[2],
                'type': line[3],
                'total_credit': line[4],
                'course_code': line[5],
                'mark': line[6],
                'eqvl': line[7],
                'total_credit_course': line[8],
            }
            details.append(detail_data)
        total_credit_required = frame_results.get('total_credit_required', 0)
        total_credit_gained = frame_results.get('total_credit_gained', 0)
        finished = frame_results.get('finished', False)
        percentage = frame_results.get('percentage', '0.0%')
        res.update({'details': details, 
                    'student_id': student_id, 
                    'total_credit_required': total_credit_required, 
                    'total_credit_gained': total_credit_gained,
                    'finished': finished,
                    'percentage': percentage})
        return res
    
    
    def default_get(self, cr, uid, fields, context=False):
        res = super(aas_frame_result, self).default_get(cr, uid, fields, context=context)
        id = context.get('active_id', False)
        res_compute = self.compute(cr, uid, id, context=context)
        res.update(res_compute)
        res.update({'readonly': True})
        return res
        
aas_frame_result()

# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
